home *** CD-ROM | disk | FTP | other *** search
/ BBS in a Box 7 / BBS in a Box - Macintosh - Volume VII (BBS in a Box) (January 1993).iso / Files / Prog / M / Lex.cpt / Lex / ObjectC.lxi < prev   
Text File  |  1990-06-05  |  2KB  |  138 lines

  1. /*
  2.  * C lex
  3.  */
  4. %{
  5. #include    <stdlib.h>
  6. #include    <console.h>
  7. char    *yyval;
  8. %}
  9. digit    = [0-9];
  10. letter    = [a-zA-Z_];
  11. name    = letter(letter|digit)*;
  12. integer = digit digit*;
  13. any        = [\0-\377];
  14. white    = [ \t\n];
  15. blanks    = white*;
  16. %{
  17. main(int argc, char **argv)
  18. {
  19.     register int    i, len, index;
  20.     char            buffer[80];
  21.     extern char        *token();
  22.     extern FILE *    lexin;
  23. #ifdef    THINK_C
  24.     console_options.nrows = 10;
  25.     cecho2file("clex log", 0, stdout);
  26.     cecho2file("clex log", 1, stderr);
  27. /*    argc = ccommand(&argv); */
  28.     lexin = fopen("test.c", "r");
  29.     if(lexin == NULL)
  30.         Debugger();
  31. #endif
  32.  
  33.     while (i = yylex()) {
  34.         if (i == LEXERR) {
  35.             Debugger();
  36.             error("LEXERR -- abort");
  37.             break;
  38.         }
  39.     }
  40.     putchar('\n');
  41. }
  42. %}
  43. %%
  44. %{
  45.     static short blevel = 0, structdef = 0;
  46.     short        c;
  47. %}
  48. #INCLUDE    {                    /* preprocessor directives */
  49.             printf("\nincluding : ");
  50.             while((c = lexchar()) != '<' && c != '\"')
  51.                 ;
  52.             putchar(c);
  53.             while((c = lexchar()) != '>' && c != '\"')
  54.                 putchar(c);
  55.             putchar(c); putchar('\n');
  56.             return(LEXSKIP);
  57.             }
  58.  
  59. "#"            {                    /* ignore other preprocessor directives */
  60.             comment("\n");
  61.             return(LEXSKIP);
  62.             }
  63. "{"            {
  64.             blevel++;
  65.             return(LEXSKIP);
  66.             }
  67. "}"            {
  68.             blevel--;
  69.             if(blevel == 0)
  70.                 structdef = 0;
  71.             return(LEXSKIP);
  72.             }
  73. STRUCT        {
  74.             if(blevel == 0)
  75.                 structdef = 1;
  76.             return(LEXSKIP);
  77.             }
  78. name / blanks ":"    {
  79.             if(structdef) {
  80.                 printf("\nclass def:");
  81.                 structdef = 2;
  82.                 }
  83.             lexecho(stdout);
  84.             }
  85. name / blanks "{"    {
  86.             switch(structdef) {
  87.                 case 0:
  88.                     printf("\nunrecognized:");
  89.                     break;
  90.                 case 1:
  91.                     printf("\nordinary struct:");
  92.                     break;
  93.                 case 2:
  94.                     printf("\nsuperclass:");
  95.                     break;
  96.                 }
  97.             lexecho(stdout);
  98.             }            
  99. name / blanks "("    {
  100.             switch(structdef) {
  101.                 case 0:
  102.                     printf("\nmatched function %s:",
  103.                     blevel>0 ? "use" : "declaration");
  104.                     break;
  105.                 case 2:
  106.                     printf("\nmethod declaration:");
  107.                     break;
  108.                     }
  109.             lexecho(stdout);
  110.             return(1);
  111.             }
  112. name        {
  113.             printf("\nmatched name :\n");
  114.             lexecho(stdout);
  115.             return(2);
  116.             }
  117. integer        {
  118.             printf("\nmatched integer:\n");
  119.             lexecho(stdout);
  120.             return(3);
  121.             }
  122. "/*"        {
  123.             comment("*/");
  124.             return(LEXSKIP);
  125.             }
  126. any            {
  127.             return(LEXSKIP);
  128.             }
  129. %%
  130.  
  131. error(char *s) {
  132.     fprintf(stderr, s);
  133.     exit(1);
  134.     }
  135.  
  136. void yyinit() {};
  137.  
  138.